﻿<%@ WebHandler Language="C#" Class="CommunityInfo" %>

using System;
using System.Web;
using System.Data;
using Chenxin.Lib.Utility;

public class CommunityInfo : AjaxHandlerBase {
    
    override public void RequestProcess (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";

        if (context.Request["op"] != null)
        {
            switch (context.Request["op"].ToLower())
            {
                case "getmodel":
                    GetModel(context);
                    break;
                case "getlist":
                    GetList(context);
                    break;
            }
        }
    }

    private void GetModel(HttpContext context)
    {
        try
        {
            Chenxin.Lib.House.Entity.Community_BaseInfo model = null;

            string strCachKey = string.Format(Chenxin.Lib.Cache.CacheDataType.HOUSE_COMMUNITYINFO, context.Request["communityid"]);
            Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);
            model = cs.GetObject<Chenxin.Lib.House.Entity.Community_BaseInfo>(strCachKey);
            if (!cs.Hit)
            {
                model = Chenxin.Lib.House.DAL.Community_BaseInfo.GetModel(new Guid(context.Request["communityid"]));
                if (model != null)
                {
                    cs.Set(strCachKey, model, DateTime.Now.AddDays(1));
                }
            }


            if (model != null)
            {
                context.Response.Write(("{success:'1',msg:'',data:{model:" + JsonHelp.Convert2Json(model) + "}}"));
            }
            else
            {
                context.Response.Write("{success:'0',msg:'获取记录信息失败！',data:''}");
            }
        }
        catch
        {
            context.Response.Write(("{success:'0',msg:'获取记录信息失败！',data:{model:{}}}"));
        }
    }

    private void GetList(HttpContext context)
    {        
            string strWhere = "1=1 ";
            string order = "Community_BuildingInfo.BuildingName ";

            int pageSize = 10;
            int pageIndex = 1;
            int count = 0;

            if (context.Request["pagesize"] != null)
            {
                pageSize = Convert.ToInt32(context.Request["pagesize"]);
            }
            if (context.Request["pageindex"] != null)
            {
                pageIndex = Convert.ToInt32(context.Request["pageindex"]) + 1;
            }

            if (context.Request["communityid"] != null)
            {
                strWhere += string.Format(" and communityid='{0}'", context.Request["communityid"]);
            }

            if (context.Request["sort"] != null && context.Request["sort"]!="")
            {
                order = context.Request["sort"];
            }

            if (context.Request["roomrule"] != null)
            {
                strWhere += string.Format(" and RoomRule='{0}'", context.Request["roomrule"]);
            }
            if (context.Request["status"] != null)
            {
                strWhere += string.Format(" and Status in ({0})", context.Request["status"]);
            }
            if (context.Request["buildingname"] != null)
            {
                strWhere += string.Format(" and BuildingName like '%{0}%'", context.Request["buildingname"]);
            }
        
            //strWhere += string.Format(" and Status <> {0}", -1);

            DataSet ds = null;

            if (context.Request["communityid"] != null && pageIndex == 1 && strWhere == "1=1")
            {
                string strCachKey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_COMMUNITY_BUILDINGS, context.Request["communityid"] + "_1_50");
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);
                ds = cs.GetObject<DataSet>(strCachKey);
                if (!cs.Hit)
                {
                    ds = Chenxin.Lib.House.DAL.Community_BaseInfo.GetList(pageIndex, pageSize, order, "Community_BuildingInfo", strWhere, "*", "*", true);
                    if (ds != null)
                    {
                        cs.Set(strCachKey, ds, DateTime.Now.AddDays(1));
                    }
                }
            }
            else
            {
                ds = Chenxin.Lib.House.DAL.Community_BaseInfo.GetList(pageIndex, pageSize, order, "Community_BuildingInfo", strWhere, "*", "*", true);
            }
            count = Convert.ToInt32(ds.Tables[1].Rows[0].ItemArray[0]);

            context.Response.Write(string.Format("{{success:'1',data:{{html:'{0}',count:{1}}}}}", CreateGrid(ds.Tables[0].Select("1=1")), count));
        
    }

    public string CreateGrid(DataRow[] drList)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();


        foreach (DataRow dr in drList)
        {
            sb.AppendFormat("<tr id=\"{0}\">", dr["BuildingId"]);
            sb.AppendFormat("<td>{0}</td>", dr["BuildingName"]);
            sb.AppendFormat("<td>{0}</td>", dr["Floor"]);
            sb.AppendFormat("<td>{0}</td>", dr["UnitNum"]);
            sb.AppendFormat("<td>{0}</td>", dr["UnitType"]);
            sb.AppendFormat("<td>{0}</td>", dr["RoomRuleRemark"]);
            sb.AppendFormat("<td>{0}</td>", dr["AddPerson"]);
            sb.AppendFormat("<td>{0}</td>", Convert.ToDateTime(dr["AddTime"]).ToString("yyyy-MM-dd HH:mm:ss"));

            if (dr["Status"].ToString() == "-1")
            {
                sb.Append("<td style=\"text-align:center;\"><span style=\"color:#ff0000;\">已删除</span></td>");
            }
            else if (dr["Status"].ToString() == "-10")
            {
                sb.Append("<td style=\"text-align:center;\"><span style=\"color:#0000ff;\">已冻结</span></td>");
            }
            else
            {
                sb.Append("<td style=\"text-align:center;\"><span style=\"color:#009900;\">正常</span></td>");
            }
            sb.Append("</tr>");
        }


        return sb.ToString();
    }
}